var csv = require('./lib/ya-csv')
var path = require('path')
var fs = require('fs')
var lotteryData;

function lotteryDataInit() {
    lotteryData = {
        currentStep: -1,
        items: [],
        videos: [
            './assets/WC 1A.mp4',
            './assets/WC 1B.mp4',
            './assets/WC 1C.mp4',
            './assets/WC 1D.mp4'
        ],
        lotteryResult: [

        ]
    }
}


function loadData() {
    var curPath = path.dirname(path.resolve(process.execPath));
    var srcPath;
    if (!fs.existsSync(curPath + '/srcData/DataA.csv')) {
        srcPath = './data/DataA.csv'; 
    }
    else {
        srcPath = curPath + '/srcData/DataA.csv'
    }
    var reader = csv.createCsvFileReader(srcPath, {
        'separator': ',',
        'quote': '"',
        'escape': '"',
        'comment': '',
    });
    reader.addListener('data', function (data) {
        lotteryData.items.push(data)
    });
}

function writeData(dataArr) {
    var curPath = path.dirname(path.resolve(process.execPath));
    if (!fs.existsSync(curPath + '/data')) {
        fs.mkdirSync(curPath + '/data')
    }
    //var writer = csv.createCsvFileWriter(curPath + '/data/dataResultA.csv');
    // var csvRecord = '';
    // lotteryData.lotteryResult.forEach(dateItem => {
    //     csvRecord += dateItem[0].replace(/,/g,' ') + ',' + dateItem[1] + '\n'
    // })
    // writer.writeRecord(csvRecord);
    fs.appendFileSync(curPath + '/data/dataResultA.csv', dataArr[0].replace(/,/g, ' ') + ',' + dataArr[1] + ',' + (new Date().toLocaleString()) + '\n')
}

function deleteData() {
    var curPath = path.dirname(path.resolve(process.execPath));
    if (fs.existsSync(curPath + '/data/dataResultA.csv')) {
        fs.unlinkSync(curPath + '/data/dataResultA.csv')
    }
}

lotteryDataInit()
loadData()

var lotteryVideoIndex = 0;
var lotterVideoMaxIndex = lotteryData.videos.length;//此处不减1 后面判断则不需减一
function getVideo() {
    var result = lotteryData.videos[lotteryVideoIndex]
    if (lotteryVideoIndex + 1 < lotterVideoMaxIndex) {
        lotteryVideoIndex++;
    } else {
        //若溢出则回归0
        lotteryVideoIndex = 0;
    }
    return result;
}
var isRunning = false;
var isReady = true;
function nextStep() {
     if (!isRunning) {
    //     if (!isReady) {
    //         var videoElement = document.querySelector('#mainVideo')
    //         videoElement.src = ''
    //         videoElement.src = './assets/hand_button.mp4'
    //         showResult(0);
    //         isReady = true;
    //     } else {
            showResult(0);
            isRunning = true;
            var videoElement = document.querySelector('#mainVideo')
            videoElement.currentTime = 1
            videoElement.play();
            setTimeout(() => {
                videoEnded();
            }, 5 * 1000);
        // }
    }
}
var videoState = 'wordcloud'||'handbutton' 
function mainVideoEnded() {
    switch (videoState) {
        case "handbutton":
            playWordcloud()
            videoState = 'wordcloud'
            break;
        case "wordcloud": 
            isRunning = false;
            isReady = false;
            //videoState = 'handbutton'
            break;
    }
}

function playWordcloud() {
    showResult(0);
    lotteryData.currentStep++;
    var videoUrl = getVideo();
    var videoElement = document.querySelector('#mainVideo')
    videoElement.src = videoUrl;
    videoElement.play();
    setTimeout(() => {
        videoEnded();
    }, 7 * 1000);
}


function reSet() {
    if (confirm("Are you sure reset")) {
        showResult(0);
        lotteryDataInit();
        loadData();
        var videoElement = document.querySelector('#mainVideo')
        videoElement.src = './assets/hand_button.mp4';
        // videoElement.setAttribute('loop')
        isPlaying = false;
        isReady = true
        deleteData()
    }
}

function videoEnded() {
    var resultData = computLottery(lotteryData.items)
    lotteryData.items = resultData.data
    lotteryData.lotteryResult.push(resultData.lotteryData[0])
    showResult(1, resultData.lotteryData[0]);
    writeData(resultData.lotteryData[0])
}

function showResult(isShow, text) {
    var resultElement = document.querySelector('#displayResult')
    if (isShow) {
        resultElement.classList.add('showResultAnimation')
        resultElement.style['display'] = 'block'
        var name = text[0]
        var sid = text[1]
        setTimeout(() => {
            resultElement.innerHTML = "<h2><b>" + name + "</b></h2>";
            resultElement.style['min-width'] = '800px'
            resultElement.style['min-height'] = '250px'
        }, 1000);
    } else {
        resultElement.classList.remove('showResultAnimation')
        resultElement.style['display'] = 'none'
        resultElement.style['min-width'] = '0'
        resultElement.style['min-height'] = '0'
        resultElement.innerHTML = ''
    }
}


